package com.agricultural.machine.observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 环境监测器类 - 观察者模式的被观察者
 */
public class EnvironmentMonitor {
    private static EnvironmentMonitor instance;
    private final List<IEnvironmentalObserver> observers;
    private final Random random;
    private final ScheduledExecutorService scheduler;
    private boolean running;
    
    private EnvironmentMonitor() {
        this.observers = new ArrayList<>();
        this.random = new Random();
        this.scheduler = Executors.newScheduledThreadPool(1);
        this.running = false;
    }
    
    public static synchronized EnvironmentMonitor getInstance() {
        if (instance == null) {
            instance = new EnvironmentMonitor();
        }
        return instance;
    }
    
    /**
     * 添加观察者
     */
    public void addObserver(IEnvironmentalObserver observer) {
        if (!observers.contains(observer)) {
            observers.add(observer);
            System.out.println("添加环境数据观察者");
        }
    }
    
    /**
     * 移除观察者
     */
    public void removeObserver(IEnvironmentalObserver observer) {
        observers.remove(observer);
        System.out.println("移除环境数据观察者");
    }
    
    /**
     * 通知所有观察者
     */
    private void notifyObservers(EnvironmentData data) {
        for (IEnvironmentalObserver observer : observers) {
            observer.updateEnvironmentData(data);
        }
    }
    
    /**
     * 开始监测
     */
    public void startMonitoring() {
        if (!running) {
            running = true;
            scheduler.scheduleAtFixedRate(this::generateEnvironmentData, 0, 5, TimeUnit.SECONDS);
            System.out.println("环境监测器已启动，开始收集数据...");
        }
    }
    
    /**
     * 停止监测
     */
    public void stopMonitoring() {
        if (running) {
            running = false;
            scheduler.shutdown();
            System.out.println("环境监测器已停止");
        }
    }
    
    /**
     * 生成环境数据
     */
    private void generateEnvironmentData() {
        String[] dataTypes = {"土壤", "气象", "作物"};
        String[] locations = {"东区", "西区", "南区", "北区"};
        
        String dataType = dataTypes[random.nextInt(dataTypes.length)];
        String location = locations[random.nextInt(locations.length)];
        
        double soilMoisture = 30 + random.nextDouble() * 40;
        double soilPH = 5.5 + random.nextDouble() * 2.0;
        double temperature = 15 + random.nextDouble() * 20;
        double humidity = 40 + random.nextDouble() * 50;
        double windSpeed = random.nextDouble() * 10;
        
        EnvironmentData data = new EnvironmentData(
            dataType, location, soilMoisture, soilPH, temperature, humidity, windSpeed
        );
        
        System.out.println("生成新的环境数据: " + data);
        notifyObservers(data);
    }
    
    /**
     * 手动生成环境数据
     */
    public void manualGenerateData(String dataType, String location) {
        double soilMoisture = 30 + random.nextDouble() * 40;
        double soilPH = 5.5 + random.nextDouble() * 2.0;
        double temperature = 15 + random.nextDouble() * 20;
        double humidity = 40 + random.nextDouble() * 50;
        double windSpeed = random.nextDouble() * 10;
        
        EnvironmentData data = new EnvironmentData(
            dataType, location, soilMoisture, soilPH, temperature, humidity, windSpeed
        );
        
        System.out.println("手动生成环境数据: " + data);
        notifyObservers(data);
    }
}